https://leetcode.cn/problems/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k/submissions/515883651/


class Solution {
public:
    int minOperations(int k) {

        int cnt = 0;

        if (k == 1)
            return 0;

        if (k == 2)
            return 1;

        if (k == 3)
            return 2;

        if (k % 2 == 0)
            cnt = k / 2;

        int ans = 2e5 + 10;
        for (int i = 2; i <= k / 2; i++)
        {
            int cnt = 0;
            if (k % i == 0)
                cnt = i - 1 + (k / i) - 1;
            else
                cnt = i - 1 + k / i;

            ans = min(ans, cnt);
        }
        return ans;
    }
};